/*********************************************************************\
*
*                  (c) lkc
*                  All Rights Reserved. lkc Confidential
*
\*********************************************************************/
/*********************************************************************\
*   Title                      :
*
*   Module Description         :
*
*   Author                     : lkc
*
*   Created                    : 2018-7-2
*
\*********************************************************************/

#include <QSettings>
#include <QDebug>
#include <thread>
#include "CamGainControl.h"
#include "ProtocolMCU.h"
#include "DisplayControl.h"
#include "CamControl.h"
#include "FocusControl.h"


QSettings settings("/home/root/rescue.ini", QSettings::IniFormat);
const std::string GAIN_SRT = "gain";
const std::string GAIN_MODE_SRT = "gainMode";
const std::string FOCUS_SRT = "focus";
const std::string CONTRAST_SRT = "contrast";
const std::string CROSS_SRT = "cross";
const std::string OUT_MODE_SRT = "outMode";
const std::string WORK_MODE_SRT = "workMode";

static void SaveGain()
{
    settings.setValue(GAIN_SRT.c_str(),CamGainControl::GetInstance().GetGain());
}

static void SaveGainMode()
{
    settings.setValue(GAIN_MODE_SRT.c_str(),CamGainControl::GetInstance().GetCamGainMode());
}

static void SaveFocus()
{
    settings.setValue(FOCUS_SRT.c_str(),focusCtl.GetFocus());
}

static void SaveContrast()
{
    settings.setValue(CONTRAST_SRT.c_str(),GetContrast());
}

static void SaveCross()
{
    settings.setValue(CROSS_SRT.c_str(),GetCrossMode());
}

static void SaveOutMode()
{
    settings.setValue(OUT_MODE_SRT.c_str(),static_cast<int>(GetOutMode()));
}

static void SaveWorkMode()
{
    settings.setValue(WORK_MODE_SRT.c_str(),CamControl::GetInstance().GetWorkMode());
}

static void Save()
{
    SaveGain();
    SaveGainMode();
    SaveFocus();
    SaveContrast();
    SaveCross();
    SaveOutMode();
    SaveWorkMode();
}

static void SaveThread()
{
    std::thread([]()
    {
        while(true)
        {
            Save();
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        }

    }).detach();
}

void InitStatusFromSave()
{
    if(settings.contains(GAIN_SRT.c_str()))
    {
        CamGainControl::GetInstance().SetGain(settings.value(GAIN_SRT.c_str()).toInt());
    }
    if(settings.contains(GAIN_MODE_SRT.c_str()))
    {
        CamGainControl::GetInstance().SetGainAutoMode(settings.value(GAIN_MODE_SRT.c_str()).toBool());
    }
    if(settings.contains(FOCUS_SRT.c_str()))
    {
        focusCtl.SetFocus(settings.value(FOCUS_SRT.c_str()).toUInt());
    }
    else
    {
        focusCtl.SetFocus(50);//默认切换短焦
    }
    if(settings.contains(CONTRAST_SRT.c_str()))
    {
        SetContrast(settings.value(CONTRAST_SRT.c_str()).toInt());
    }
    if(settings.contains(CROSS_SRT.c_str()))
    {
        SetCrossMode(settings.value(CROSS_SRT.c_str()).toBool());
    }
    if(settings.contains(OUT_MODE_SRT.c_str()))
    {
        SetOutMode(OutMode(settings.value(OUT_MODE_SRT.c_str()).toInt()));
    }
    if(settings.contains(WORK_MODE_SRT.c_str()))
    {
        CamControl::GetInstance().SetWorkMode(settings.value(WORK_MODE_SRT.c_str()).toBool());
    }
    SaveThread();
}

/********************************************************************\
 *
 * REVISION RECORDS
 *
\*********************************************************************/
/*********************************************************************/
/*
 *
 *
 *
\*********************************************************************/
/*------------------------------The End------------------------------*/
